package com.sogoodsoft.service.project;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sogoodsoft.dao.common.CommonHibernateDao;
import com.sogoodsoft.dao.project.AssessmentDao;
import com.sogoodsoft.dao.project.ProjectDao;
import com.sogoodsoft.entity.app.Organization;
import com.sogoodsoft.entity.project.Assessment;
import com.sogoodsoft.entity.project.Project;
import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.service.common.CommunicateService;

@Component
@Transactional
public class AssessmentService extends AbstractService<Assessment,Long> {
	private AssessmentDao dao;
	private ProjectDao pdao;
	private CommunicateService comm;
	
	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public CommonHibernateDao getDao() {
		return dao;
	}

	@Autowired
	public void setDao(AssessmentDao dao) {
		this.dao = dao;
	}
	
	
	public CommunicateService getComm() {
		return comm;
	}

	@Autowired
	public void setComm(CommunicateService comm) {
		this.comm = comm;
	}

	public void save(Assessment assessment){
		
		//保存当前申核
		dao.save(assessment);
		
		//更新项目通过审核的数量
		Project prj = assessment.getProject();
		prj.setAlready(prj.getAlready()+1);
		pdao.save(prj);
		
		//计算进入下一级别的审核
		
		//如果未通过本级审核，不用再申请下一级别的审核
		if(!"ASSESSMENT_RESULT_PASSED".equals(assessment.getSysClass().getCode())){
			notice(assessment);
			return;
		}
		
		//当前申核级别
		String level = assessment.getOrganization().getType().trim();
		
		//最终级别
		String target = assessment.getProject().getSysClass().getValue().trim();
		
		//进入下一级别
		if(!level.equals(target)){
			Organization org = assessment.getOrganization().getParent();
			if("-1".equals(org.getType()))org = org.getParent();
			Assessment next = new Assessment();
			next.setOrganization(org);
			next.setProject(assessment.getProject());
			dao.save(next);
			notice(assessment);
		}else{
			notice(assessment,"success");
		}
	}
	
	/**
	 * 根据当前申核结果发送通知
	 * @param assessment
	 */
	public void notice(Assessment assessment,String...type){
		String code = assessment.getSysClass().getCode();
		String mail = assessment.getProject().getUser().getEmail();
		String phone = assessment.getProject().getUser().getPhone();
		String prj = assessment.getProject().getPrjName();
		String org = assessment.getOrganization().getName();
		String subject;
		String msg;
		String sms;
		String now  = new SimpleDateFormat("yyyy年MM月dd日HH时mm分").format(new Date());
		
		if(type.length!=0&&"success".equals(type[0])){
			//邮件
			subject = "审核通过";
			msg = "恭喜您，您的项目《"+prj+"》已于"+now+"通过了"+org+"的审核，项目已通过所有机构审核！\n此邮件为系统自动发送，请勿回复！";
			comm.sendMail(assessment,subject, msg, mail);
			
			//短信
			sms = "恭喜您，您的项目《"+prj+"》已于"+now+"通过了"+org+"的审核，项目已通过所有机构审核！";
			comm.sendSMS(sms, phone);
			return;
		}
		
		if(AssessmentResult.PASSED.equalsIgnoreCase(code)){
			//审核通过
			
			//邮件
			subject = "审核通过";
			msg = "恭喜您，您的项目《"+prj+"》已于"+now+"通过了"+org+"的审核！\n此邮件为系统自动发送，请勿回复！";
			comm.sendMail(assessment,subject, msg, mail);
			
			//短信
			sms = "恭喜您，您的项目《"+prj+"》已于"+now+"通过了"+org+"的审核！";
			comm.sendSMS(sms, phone);
			
			
		}else if(AssessmentResult.FAILED.equalsIgnoreCase(code)){
			//审核未通过
			//邮件
			subject = "审核未通过";
			msg = "很遗憾，您的项目《"+prj+"》未通过"+org+"的审核！\n此邮件为系统自动发送，请勿回复！";
			comm.sendMail(assessment,subject, msg, mail);
			
			//短信
			sms = "很遗憾，您的项目《"+prj+"》未通过"+org+"的审核！";
			comm.sendSMS(sms, phone);
		}else if(AssessmentResult.WAIT.equalsIgnoreCase(code)){
			//审核挂起
			
			//邮件
			subject = "审核挂起";
			msg = "很遗憾，您的项目《"+prj+"》在"+org+"的审核中被挂起！\n此邮件为系统自动发送，请勿回复！";
			comm.sendMail(assessment,subject, msg, mail);
			
			//短信
			sms = "很遗憾，您的项目《"+prj+"》在"+org+"的审核中被挂起！";
			comm.sendSMS(sms, phone);
			
		}else if(AssessmentResult.REASSESS.equalsIgnoreCase(code)){
			//重新审核
			//邮件
			subject = "重新审核";
			msg = "很遗憾，您的项目《"+prj+"》在"+org+"的审核中被批准重新审核！\n此邮件为系统自动发送，请勿回复！";
			comm.sendMail(assessment,subject, msg, mail);
			
			//短信
			sms = "很遗憾，您的项目《"+prj+"》在"+org+"的审核中被批准重新审核！";
			comm.sendSMS(sms, phone);
		}
	}

	
	public ProjectDao getPdao() {
		return pdao;
	}

	@Autowired
	public void setPdao(ProjectDao pdao) {
		this.pdao = pdao;
	}

	
}
